﻿// Copyright richyp 2009
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Richyp.Bodger.UI {
    public class AttributeRow : DataGridViewRow {
        public Unit Unit { get; set; }

        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow) {
            graphics.DrawRectangle(SystemPens.ControlLightLight, rowBounds);
            Brush textColor = SystemBrushes.ControlText;
            if (this.Selected) {
                LinearGradientBrush lgb = new LinearGradientBrush(rowBounds,SystemColors.ActiveCaption, SystemColors.GradientActiveCaption,90);
                graphics.FillRectangle(lgb, rowBounds);
                textColor = SystemBrushes.ActiveCaptionText;
            } else {
                graphics.FillRectangle(SystemBrushes.Control, rowBounds);
            }
            graphics.DrawLine(SystemPens.ControlDarkDark, rowBounds.X, rowBounds.Y, rowBounds.X + rowBounds.Width, rowBounds.Y);
            graphics.DrawLine(SystemPens.ControlDarkDark, rowBounds.X + rowBounds.Width, 0, rowBounds.X + rowBounds.Width, rowBounds.Y + rowBounds.Height);
            graphics.DrawString(Unit.Name, SystemFonts.SmallCaptionFont, textColor, 2, rowBounds.Y);
            Font f = new Font(SystemFonts.SmallCaptionFont, FontStyle.Regular);
            int x = 2;
            bool invlunerableSaveInBase = false;
            foreach (Attribute attr in Unit.Base.Attributes) {
                if (attr.Name.ToUpper() == "INV")
                    invlunerableSaveInBase = true;

                graphics.DrawString(attr.Name.ToUpper(), SystemFonts.SmallCaptionFont, textColor, x, rowBounds.Top + 15);
                graphics.DrawLine(SystemPens.ControlDark, rowBounds.X + x, rowBounds.Top + 15, rowBounds.X + x, rowBounds.Top + 45);
                string value = this.Unit.GetAttributeString(attr.Name);
                value = value.Replace("(", "/");
                value = value.Replace(")", "");
                if (value == "0") {
                    value = "NA";
                }

                graphics.DrawString(value, f, textColor, rowBounds.X + x, rowBounds.Top + 30);
                x += 20;
            }
            if (!invlunerableSaveInBase && this.Unit.GetAttributeString("inv") != "")
            {            
                graphics.DrawString("INV", SystemFonts.SmallCaptionFont, textColor, x, rowBounds.Top + 15);
                graphics.DrawLine(SystemPens.ControlDark, rowBounds.X + x, rowBounds.Top + 15, rowBounds.X + x, rowBounds.Top + 45);
                string value = this.Unit.GetAttributeString("inv");
                graphics.DrawString(value, f, textColor, rowBounds.X + x, rowBounds.Top + 30);
                x += 20;
            }
            graphics.DrawLine(SystemPens.ControlDark, rowBounds.X + x, rowBounds.Top + 15, rowBounds.X + x, rowBounds.Top + 45);
            graphics.DrawLine(SystemPens.ControlDark, rowBounds.X + 2, rowBounds.Y + 15, rowBounds.X + x, rowBounds.Y + 15);
            graphics.DrawLine(SystemPens.ControlDark, rowBounds.X + 2, rowBounds.Y + 30, rowBounds.X + x, rowBounds.Y + 30);
            graphics.DrawLine(SystemPens.ControlDark, rowBounds.X + 2, rowBounds.Y + 45, rowBounds.X + x, rowBounds.Y + 45);
        }
    }
}
